Skip to content

Add stubs for the stdlib _colorize module - #16370

Open
anistark wants to merge 1 commit into
python:mainfrom
anistark:add-colorize-stubs
Open

Add stubs for the stdlib _colorize module#16370
anistark wants to merge 1 commit into
python:mainfrom
anistark:add-colorize-stubs

Conversation

@anistark

@anistark anistark commented Sep 9, 2026

Copy link
Copy Markdown

_colorize has been available since Python 3.13 and gained the experimental theming API in 3.14, which was extended considerably in 3.15. The stub covers all three versions:

  • 3.13+: COLORIZE, ANSIColors, NoColors, get_colors, can_colorize
  • 3.14+: ColorCodes, decolor, ThemeSection, the Argparse, Syntax, Traceback and Unittest sections, Theme, get_theme and set_theme
  • 3.15+: CursesColors, BackgroundStyle, ten further theme sections, and the kw_only dataclass signatures that 3.15 switched to

attr and code are loop variables that leak into the module namespace, and BackgroundStyle is declared with the type statement, which stubtest compares against the alias value; all three are allowlisted.

Closes #16361

`_colorize` has been available since Python 3.13 and gained the
experimental theming API in 3.14, which was extended considerably in
3.15. The stub covers all three versions:

- 3.13+: `COLORIZE`, `ANSIColors`, `NoColors`, `get_colors`, `can_colorize`
- 3.14+: `ColorCodes`, `decolor`, `ThemeSection`, the `Argparse`, `Syntax`,
  `Traceback` and `Unittest` sections, `Theme`, `get_theme` and `set_theme`
- 3.15+: `CursesColors`, `BackgroundStyle`, ten further theme sections, and
  the `kw_only` dataclass signatures that 3.15 switched to

`attr` and `code` are loop variables that leak into the module namespace,
and `BackgroundStyle` is declared with the `type` statement, which stubtest
compares against the alias value; all three are allowlisted.

Closes python#16361
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a full review yet, but a few things I noticed. In general, the stub file should use the same ordering as the implementation, even if it means repeating version info checks.

Comment thread stdlib/_colorize.pyi
BOLD: str
GREY: str

NoColors: ANSIColors

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NoColors: ANSIColors
NoColors: Final[ANSIColors]

Comment thread stdlib/_colorize.pyi
Comment on lines +61 to +62
if sys.version_info >= (3, 14):
ColorCodes: set[str]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this directly above NoColors so it matches the source order. Also let's make it final.

Comment thread stdlib/_colorize.pyi
Comment on lines +58 to +59
def get_colors(colorize: bool = False, *, file: IO[str] | IO[bytes] | None = None) -> ANSIColors: ...
def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IO should be avoided in argument positions. In this case it should be a protocol with one optional item fileno. Since optional protocol items are not yet supported (see python/typing#601), the best we can do is probably something like this:

Suggested change
def get_colors(colorize: bool = False, *, file: IO[str] | IO[bytes] | None = None) -> ANSIColors: ...
def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool: ...
# A protocol with an optional `fileno(self) -> int: ...` member.
_MaySupportFileno: TypeAlias = Any
def get_colors(colorize: bool = False, *, file: _MaySupportFileno | None = None) -> ANSIColors: ...
def can_colorize(*, file: _MaySupportFileno | None = None) -> bool: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add stubs for stdlib _colorize module

2 participants